1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4
5 public
class PlayerControls : MonoBehaviour {
6
7     
public KeyCode moveUp = KeyCode.W;
8     
public KeyCode moveDown = KeyCode.S;
9     
public float speed = 10.0f;
10     
public float boundY = 2.25f;
11     
private Rigidbody2D rb2d;
12
13     
// Use this for initialization
14     
void Start () {
15         rb2d = GetComponent<Rigidbody2D> ();
16     }
17     
18     
// Update is called once per frame
19     
void Update () {
20         
var vel = rb2d.velocity;
21         
if (Input.GetKey (moveUp)) {
22             vel.y = speed;
23         }
else if (Input.GetKey (moveDown)) {
24             vel.y = -speed;
25         }
else if (!Input.anyKey) {
26             vel.y =
0;
27         }
28         rb2d.velocity = vel;
29
30         
var pos = transform.position;
31         
if (pos.y > boundY) {
32             pos.y = boundY;
33         }
else if (pos.y < -boundY) {
34             pos.y = -boundY;
35         }
36         transform.position = pos;
37     }
38 }


Use this for initialization

Update is called once per frame



Gõ tìm kiếm nhanh...